﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web.UI.WebControls;
using ServerApi.Controllers;
using ServerApi.Models;
using TuyennvLibrary.Utl;
using TuyennvLibrary.ServerApi.Models;

namespace ServerApi.administrator
{
    public partial class RevenueSms : System.Web.UI.Page
    {
        public int stt = 1;
        public MOLogModel mologModel = new MOLogModel();
        public PartnerSMSModel partnerModel = new PartnerSMSModel();
        public int count = 0;
        protected void Page_Load(object sender, EventArgs e)
        {
            AdminController.checkLogin(Session, Request, Response);
            //AdminController.checkPermission(Page, this.GetType(), Session, Request, Response);
            if (!IsPostBack)
            {
                loadTypeShortCode();
                setShortCode();

                setCurrentdate();
                Search(1);
            }
        }
        public void loadTypeShortCode()
        {
            Account acc_current = (Account)Session[AdminController.SESSION_USERCURRENT];
            //DataTable dt = partnerModel.GetListAll("serviceid");
            List<string> list = partnerModel.GetListServiceId(acc_current.Serviceid, "serviceid");
            ddlTypeShortCode.DataSource = list;
            //ddlTypeShortCode.DataTextField = "serviceid";
            //ddlTypeShortCode.DataValueField = "serviceid";
            ddlTypeShortCode.DataBind();
        }
        public void setShortCode()
        {
            List<string> shortcodes = new List<string>();
            string typeShortcode = ddlTypeShortCode.Text;
            for (int i = 0; i <= 7; i++)
            {
                shortcodes.Add(typeShortcode.Replace("x", i.ToString()));
            }
            ddlShortcode.DataSource = shortcodes;
            ddlShortcode.DataBind();
            ddlShortcode.Items.Insert(0, new ListItem() { Text = "---Chọn tất cả---", Value = "0" });
        }
        protected void ddlTypeShortCode_SelectedIndexChanged(object sender, EventArgs e)
        {
            setShortCode();
            Search(1);
        }


        public void setCurrentdate()
        {
            txtfromdate.Text = txttodate.Text = DateTime.Today.ToString("dd/MM/yyyy");
        }

        public void Search(int page)
        {
            try
            {
                Account acc_current = (Account)Session[AdminController.SESSION_USERCURRENT];
                string syntax_sms = "";
                if (!string.IsNullOrEmpty(txtMessage.Text))
                {
                    if (acc_current.HavePermissionWith(txtMessage.Text))
                    {
                        syntax_sms = txtMessage.Text;
                    }
                    else
                    {
                        throw new Exception("Bạn không có quyền xem sản lượng của Message: " + txtMessage.Text);
                    }
                }
                else
                {
                    syntax_sms = acc_current.SyntaxSms;
                }
                //if (!string.IsNullOrEmpty(acc_current.syntax_sms))
                //{
                //    message += (!string.IsNullOrEmpty(message)) ? "|" + acc_current.syntax_sms : acc_current.syntax_sms;
                //}
                int pagesize = 30;
                mologModel.page = page;
                mologModel.pagesize = pagesize;
                stt = (page - 1) * pagesize + 1;
                DataTable dt = mologModel.Search(syntax_sms, txtCommand.Text, txtPhone.Text, ddlTypeShortCode.Text, ddlShortcode.Text, txtfromdate.Text, txttodate.Text);
                GrvListAcc.DataSource = dt;
                GrvListAcc.DataBind();
                pager1.ItemCount = mologModel.Count;
                count = mologModel.Count;
                pager1.CurrentIndex = page;
                pager1.PageSize = pagesize;
            }
            catch (Exception ex)
            {
                Utilities.Alert(this, this.GetType(), ex.Message);
            }
        }
        //protected void GrvListAcc_RowDataBound(object sender, GridViewRowEventArgs e)
        //{
        //    if (e.Row.RowType == DataControlRowType.DataRow)
        //    {
        //        LinkButton l = (LinkButton)e.Row.FindControl("lbtnDel");
        //        l.Attributes.Add("onclick", "javascript:return " +
        //        "confirm('Bạn có muốn xóa tin nhắn này không? " + "')");
        //    }
        //}

        //protected void GrvListAcc_RowDeleting(object sender, GridViewDeleteEventArgs e)
        //{
        //    int id = int.Parse(GrvListAcc.DataKeys[e.RowIndex].Value.ToString());
        //    if (mologModel.Delete(id))
        //    {
        //        Search(1);
        //        Utilities.Alert(Page, this.GetType(), "Đã xóa tin nhắn thành công");
        //    }
        //}

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Search(1);
        }

        protected void btnDeleteSelecteds_Click(object sender, EventArgs e)
        {
            string[] listID = Request.Form.GetValues("chk[]");
            mologModel.DeleteMulti(listID);
            Search(1);
        }

        protected void pager1_Command(object sender, CommandEventArgs e)
        {
            int currnetPageIndx = Convert.ToInt32(e.CommandArgument);
            Search(currnetPageIndx);
        }
    }
}